package cn.ziyoufa.smart.smartschool.net;

import android.os.AsyncTask;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import cn.ziyoufa.smart.smartschool.constant.Constant;

/**
 * 创建人 ： 赵泽东
 * 创建时间：2018/9/30
 *
 * @param
 * @param
 */
public class NetConnection {
    public NetConnection(final String url, final HttpMethod method, final SuccessCallback successCallback, final FailCallback failCallback, final String ... kvs){
        /**
         * Created by ZZD on 2016/6/4.
         * Params1 启动任务执行的输入参数，比如HTTP请求的URL。
         * Params2 后台任务执行的百分比。
         * Result 后台执行任务最终返回的结果，比如String.
         */
        new AsyncTask<Void, Void, String>() {

            @Override
            protected String doInBackground(Void... arg0) {

                StringBuffer paramsStrPost = new StringBuffer();
                StringBuffer paramsStrGet = new StringBuffer();
                for (int i = 0; i < kvs.length; i+=2) {
                    paramsStrPost.append(kvs[i]).append("=").append(kvs[i+1]).append("&");
                    paramsStrGet.append(kvs[i]).append("/").append(kvs[i+1]).append("/");
                }

                try {
                    URLConnection uc;

                    switch (method) {
                        case POST:
                            uc = new URL(url).openConnection();
                            uc.setDoOutput(true);
                            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(uc.getOutputStream(), Constant.CHARSET));
                            bw.write(paramsStrPost.toString());
                            bw.flush();//立即发送到服务器
                            break;
                        default:
                            uc = new URL(url+"/"+paramsStrGet.toString()).openConnection();
                            break;
                    }

                    BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream(), Constant.CHARSET));
                    String line = null;
                    StringBuffer result = new StringBuffer();
                    while((line=br.readLine())!=null){
                        result.append(line);
                    }
                    return result.toString();

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return null;
            }

            @Override
            protected void onPostExecute(String result) {

                if (result!=null) {
                    if (successCallback!=null) {
                        successCallback.onSuccess(result);
                    }
                }else{
                    if (failCallback!=null) {
                        failCallback.onFail();
                    }
                }

                super.onPostExecute(result);
            }
        }.execute();
    }
    public static interface SuccessCallback{
        void onSuccess(String result);
    }

    public static interface FailCallback{
        void onFail();
    }
}
